﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.Utils;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmKhauTruThueBuoc3 : Form
    {
        private ButToanMgt buttoanMgt = new ButToanMgt();
        private DoiTruThueManagement doitruthueMgt = new DoiTruThueManagement();
        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();

        private int idLoaiPhieu = 3;
        private int iThangTinhThue = 0;
        private long lTien1331 = 0;
        private long lTien3331 = 0;
        private long lSoKhauTru = 0;
        private long lSoConDuocKhauTru = 0;
        private long lSoNopTrongKy = 0;
        private long lSDDK1331 = 0;
        private long lSDDK3331 = 0;

        public FrmKhauTruThueBuoc3()
        {
            InitializeComponent();
        }

        public FrmKhauTruThueBuoc3(int iThangTinhThue, long lSoKhauTru, long lTien1331, long lTien3331, long lSDDK1331, long lSDDK3331, long lSoConDuocKhauTru, long lSoNopTrongKy)
        {
            InitializeComponent();
            this.iThangTinhThue = iThangTinhThue;
            this.lSoKhauTru = lSoKhauTru;
            this.lTien1331 = lTien1331;
            this.lTien3331 = lTien3331;
            this.lSDDK1331 = lSDDK1331;
            this.lSDDK3331 = lSDDK3331;
            this.lSoConDuocKhauTru = lSoConDuocKhauTru;
            this.lSoNopTrongKy = lSoNopTrongKy;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnQuayLai_Click(object sender, EventArgs e)
        {
            int iThangTinhThue = dtpNgayHachToan.Value.Month;
            FrmKhauTruThueBuoc2 frm = new FrmKhauTruThueBuoc2(iThangTinhThue);
            this.Hide();
            frm.ShowDialog();
            this.Close();
        }

        private void btnKhauTruThue_Click(object sender, EventArgs e)
        {
            ResultObj resultObj = new ResultObj();

            DoiTruThue doitruthue = new DoiTruThue();
            doitruthue.MaSo = txtSoChungTu.Text;
            doitruthue.History = "";
            doitruthue.NgayThang = dtpNgayChungTu.Value;
            doitruthue.DienGiai = rtbDienGiai.Text;
            doitruthue.ThueGTGTDauRa3331 = lTien3331;
            doitruthue.ThueGTGTDuocKhauTru1331 = lTien1331;
            doitruthue.TienKhauTru = lSoKhauTru;

            resultObj = doitruthueMgt.SaveNewDoiTruThue(doitruthue);

            if (resultObj.ReturnCode > 0)
            {
                ButToan buttoan = new ButToan();
                buttoan.NgayThang = dtpNgayChungTu.Value;
                buttoan.ChiNhanh = "";
                buttoan.MaSoKhachHang = "";
                buttoan.DienGiai = rtbDienGiai.Text;
                buttoan.SoChungTu = txtSoChungTu.Text;
                buttoan.TKGoc = "3331";
                buttoan.TKDoiUng = "1331";
                buttoan.GhiNo = lSoKhauTru;
                buttoan.GhiCo = 0;

                resultObj = buttoanMgt.SaveNewButToan(buttoan);

                buttoan = new ButToan();
                buttoan.NgayThang = dtpNgayChungTu.Value;
                buttoan.ChiNhanh = "";
                buttoan.MaSoKhachHang = "";
                buttoan.DienGiai = rtbDienGiai.Text;
                buttoan.SoChungTu = txtSoChungTu.Text;
                buttoan.TKGoc = "1331";
                buttoan.TKDoiUng = "3331";
                buttoan.GhiNo = 0;
                buttoan.GhiCo = lSoKhauTru;

                resultObj = buttoanMgt.SaveNewButToan(buttoan);
            }

            if (resultObj.ReturnCode > 0)
            {
                MessageBox.Show("Lưu thành công", "Thông báo");
                btnQuayLai.Enabled = false;
                btnKhauTruThue.Enabled = false;
                btnIn.Enabled = true;
            }
            else if (resultObj.ReturnCode == Definitions.DUPLICATED_VALUE)
                MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
            else
                MessageBox.Show("Thất bại");
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            string sThoiGian = "Tháng " + iThangTinhThue + " năm " + DateTime.Now.Year.ToString();
            FrmXemReport frmView = new FrmXemReport(sThoiGian, lSDDK1331, lSDDK3331, lTien1331, lTien3331, lSoNopTrongKy, lSoConDuocKhauTru);
            this.Hide();
            frmView.Show();
            this.Show();
        }

        private void FrmKhauTruThueBuoc3_Load(object sender, EventArgs e)
        {
            rtbDienGiai.Text = "Khấu trừ thuế tháng " + iThangTinhThue.ToString() + " năm " + DateTime.Now.Year.ToString();
            dtpNgayChungTu.Value = new DateTime(DateTime.Now.Year, iThangTinhThue, 1).AddMonths(1).AddDays(-1);
            dtpNgayChungTu.Value = new DateTime(DateTime.Now.Year, iThangTinhThue, 1).AddMonths(1).AddDays(-1);
            txtSoChungTu.Text = "DTT/" + dtpNgayChungTu.Value.Month.ToString("MM") + dtpNgayChungTu.Value.Year.ToString("yy") + "/" + KiemTraMaSo.CheckCode("DTT", idLoaiPhieu, dtpNgayChungTu.Value);

            DataGridViewRow row = new DataGridViewRow();

            DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
            cell.Value = lTien3331 > lTien1331 ? "1331" : "3331";
            row.Cells.Add(cell);

            cell = new DataGridViewTextBoxCell();
            cell.Value = lTien3331 > lTien1331 ? "3331" : "1331";
            row.Cells.Add(cell);

            cell = new DataGridViewTextBoxCell();
            cell.Value = lSoKhauTru;
            row.Cells.Add(cell);

            cell = new DataGridViewTextBoxCell();
            cell.Value = "Khấu trừ thuế GTGT của hàng hóa, dịch vụ";
            row.Cells.Add(cell);

            dgvChiTiet.Rows.Add(row);
        }

        private void dgvChiTiet_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = true;
        }

        public List<ButToan> GetAllButToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButToan();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<DoiTruThue> GetAllDoiTruThue()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = doitruthueMgt.GetAllDoiTruThue();
            List<DoiTruThue> listDoiTruThue = (List<DoiTruThue>)resultObj.Result;

            return listDoiTruThue;
        }

        public List<TaiKhoanKeToan> GetAllTaiKhoanKeToan()
        {
            ResultObj resultObj = new ResultObj();

            List<TaiKhoanKeToan> lstTaiKhoanKeToan = new List<TaiKhoanKeToan>();

            resultObj = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            lstTaiKhoanKeToan = (List<TaiKhoanKeToan>)resultObj.Result;

            return lstTaiKhoanKeToan;
        }
    }
}
